package com.ib.algokit.forex;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import com.ib.algokit.history.ProfitLimitClosedEvent;

public class ProfitLimitClosedEventDeviationHash {
	
	private TreeMap<Double, ProfitLimitClosedEvent> cache = new TreeMap<Double, ProfitLimitClosedEvent>();
	
	public ProfitLimitClosedEventDeviationHash(List<ProfitLimitClosedEvent> profitLimitClosedEvents) {
		cache.clear();
		for(ProfitLimitClosedEvent profitLimitClosedEvent: profitLimitClosedEvents) {
			ForexPairStates forexPairStates=profitLimitClosedEvent.getForexPairStates();
			double deviation=forexPairStates.getDeviation();
			if(cache.put(deviation, profitLimitClosedEvent)!=null){
				throw new RuntimeException("percChange exists "+deviation);
			}
		}
	}
	
	public List<ProfitLimitClosedEvent> getInterval(Double start, Double end) {        
	    SortedMap<Double, ProfitLimitClosedEvent> subMap = cache.subMap(start, end);
	    List<ProfitLimitClosedEvent> results = new ArrayList<ProfitLimitClosedEvent>();
	    results.addAll(subMap.values());        
	    return results;
	}
}
